package com.rchat.pocmini.global;


import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * @Description ：
 * @Author itsdf07
 * @Time 2017/9/29
 */

public class RequestParam {

    /**
     * 系统平台：IOS - 0，Android - 1
     */
    public static final String REQUEST_PLATFORM = "1";
    /**
     * 企业平台：睿洽 ： 0，GY ：1，anytalk ：2，其他往后加
     */
    public static final String REQUEST_ENTERPRISE = AppGlobal.APPTYPE;

    /**
     * @param requestCode 接口事务码，用于动态获取接口版本号
     * @param param
     * @return
     */
    public static String requestRootParam(int requestCode, Map<String, String> param) {
        if(param == null){
            param = new HashMap<>();
        }
        JSONObject root = new JSONObject();
        try {
            param.put(HttpNodeGlobal.NodeReqPlatform, REQUEST_PLATFORM);
            param.put(HttpNodeGlobal.NodeReqVersion, HttpCodeGlobal.getUrlVersion(requestCode));
            param.put(HttpNodeGlobal.NodeReqEnterprise, REQUEST_ENTERPRISE);

            root.put(HttpNodeGlobal.NodeReqCode, requestCode);
            return root.put(HttpNodeGlobal.NodeReqData, requestParam(param)).toString();
        } catch (JSONException e) {
            return "";
        }
    }



    /**
     * build通用字段：code、platform、version、enterprise
     *
     * @param requestCode 事务码
     * @return
     * @throws JSONException
     * @modify 因为code、platform、version、enterprise移动到data内了，所以这块需要重新封装
     */
    public static JSONObject buildGeneralParam(int requestCode) throws JSONException {
        JSONObject root = new JSONObject();
        root.put(HttpNodeGlobal.NodeReqCode, requestCode);
        return root;
    }

    /**
     * @param param
     * @return
     */
    private static JSONObject requestParam(Map<String, String> param) {
        JSONObject json = new JSONObject();
        if (param != null && !param.isEmpty()) {
            for (String key : param.keySet()) {
                try {
                    json.put(key, param.get(key));
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }
        return json;
    }
}
